import {Component} from '@angular/core';
import {IonicPage, Nav, NavParams} from "ionic-angular";

interface PageInterface {
    name: string;
    index?: number;
}

@IonicPage()
@Component({
    templateUrl: 'tabs.html'
})
export class TabsPage {
    profilePage = 'ProfilePage';
    quotesPage = 'QuotesPage';
    tab0Root = 'HomePage';
    tab1Root = 'CustomListPage';
    tab2Root = 'CustomListBetaPage';
    tab3Root = 'AboutPage';
    tab4Root = 'ContactPage';
    mySelectedIndex: number;

    constructor(public navParams: NavParams, private nav: Nav) {
        this.mySelectedIndex = navParams.data.tabIndex || 0;
    }

    public openPage(page: PageInterface) {
        let params = {};

        // the nav component was found using @ViewChild(Nav)
        // setRoot on the nav to remove previous pages and only have this page
        // we wouldn't want the back button to show in this scenario
        if (page.index) {
            params = {tabIndex: page.index};
        }

        // If we are already on tabs just change the selected tab
        // don't setRoot again, this maintains the history stack of the
        // tabs even if changing them from the menu
        if (this.nav.getActiveChildNavs().length && page.index != undefined) {
            this.nav.getActiveChildNavs()[0].select(page.index);
            // Set the root of the nav with params if it's a tab index
        } else {
            this.nav.setRoot(page.name, params).catch((err: any) => {
                console.log(`Didn't set nav root: ${err}`);
            });
        }
    }
}
